import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.*;

/**
 * ViewIncomeStatement
 * Print Income Statement - for a given month, calculate number of patients seen, and total billed amount. 
 * You may display the statement on the screen rather than printing a hard copy.
 * 
 * @author bethanysumner
 *
 * @version $Revision: 1.0 $
 */
public class ViewIncomeStatement extends JPanel{
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	
	/**
	 * Field comboBox.
	 */
	private JComboBox comboBox;
	/**
	 * Field patientLabel.
	 */
	private JLabel patientLabel;
	/**
	 * Field billLabel.
	 */
	private JLabel billLabel;
	/**
	 * Field submitButton.
	 */
	private JButton submitButton;
	/**
	 * Field cancelButton.
	 */
	private JButton cancelButton;
	
	/**
	 * ViewIncomeStatement Constructor
	 * @param main Reference to the main window
	
	 * @param database Database
	 */
	public ViewIncomeStatement(MainWindow main, Database database) {
		
		this.main = main;
		this.database = database;
		
		main.showMessage("Select a month to view the income statement.");
		
		setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setBounds(97, 30, 167, 20);
		comboBox.addItem("January");
		comboBox.addItem("February");
		comboBox.addItem("March");
		comboBox.addItem("April");
		comboBox.addItem("May");
		comboBox.addItem("June");
		comboBox.addItem("July");
		comboBox.addItem("August");
		comboBox.addItem("September");
		comboBox.addItem("October");
		comboBox.addItem("November");
		comboBox.addItem("December");
		add(comboBox);
		
		JLabel lblMonth = new JLabel("Month:");
		lblMonth.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblMonth.setBounds(39, 33, 46, 14);
		add(lblMonth);
		
		
		JLabel lblNumberOfPatients = new JLabel("Number of patients seen:");
		lblNumberOfPatients.setBounds(39, 94, 167, 14);
		add(lblNumberOfPatients);
		
		JLabel lblNewLabel = new JLabel("Total Bill Amount:");
		lblNewLabel.setBounds(39, 156, 131, 14);
		add(lblNewLabel);
		
		patientLabel = new JLabel("0");
		patientLabel.setBounds(240, 94, 167, 14);
		add(patientLabel);
		
		billLabel = new JLabel("$0.00");
		billLabel.setBounds(240, 156, 167, 14);
		add(billLabel);
		
		submitButton = new JButton("Submit");
		submitButton.setBounds(276, 30, 89, 23);
		ButtonListener bl = new ButtonListener();
		submitButton.addActionListener(bl);
		add(submitButton);
		
		cancelButton = new JButton("Close");
		cancelButton.setBounds(30, 234, 152, 23);
		cancelButton.addActionListener(bl);
		add(cancelButton);
	
	}
	
	/**
	 * Removes tab from pane
	 */
	private void cancel(){
		main.clearMessage();
		main.removeTab(this);
	}
	
	/**
	 * Action listener to listen for button actions
	 * @author Zach 
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(submitButton)){
				try {
					System.out.println(comboBox.getSelectedItem().toString());
					ArrayList<Integer> s = database.printIncomeStatement(comboBox.getSelectedItem().toString());
					final double presAmount = 50.00 * s.get(0);
					final double lwAmount = 150.00 * s.get(1);
					final double patientsSeen = s.get(2);
					final double total = ((600.00 * patientsSeen) + lwAmount + presAmount);
					patientLabel.setText(patientsSeen + "");
					billLabel.setText("$" + total);
				} catch (SQLException e1) {
					main.showError("Error loading income statement.");
					main.logError(e1);
					e1.printStackTrace();
				} catch (IndexOutOfBoundsException e1){
					main.showError("Error loading income statement.");
					main.logError(e1);
					e1.printStackTrace();
				}
			}else if(e.getSource().equals(cancelButton)){
				cancel();
			}
			
		}
		
	
	}
}
